package com.onlygx.dva.web.base.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.onlygx.dva.common.entity.utils.PhoneParam;
import com.onlygx.dva.common.exception.FailRequestException;
import com.onlygx.dva.common.properties.CommonPhoneProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * <p>
 *  Email 发送服务
 * </p>
 *
 * @author 高祥
 * @since 2023-07-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonAliSmsService {

    private static final String SMS_RANDOM_CODE = "SMS_211491523";

    final CommonPhoneProperties commonPhoneProperties;

    private IAcsClient client;

    @PostConstruct
    public void init() {
        DefaultProfile profile = DefaultProfile.getProfile(commonPhoneProperties.getRegionId()
                , commonPhoneProperties.getAccessKeyId()
                , commonPhoneProperties.getSecret());
        client = new DefaultAcsClient(profile);
    }

    /**
     * 发送短信验证码
     */
    public String sendRandom6Code(String phoneNum) throws Exception {
        if (StrUtil.isEmpty(phoneNum) || !Validator.isMobile(phoneNum)) {
            throw new FailRequestException("参数格式错误");
        }

        String code = RandomUtil.randomNumbers(6);
        //发送验证码
        List<String> phoneNumbers = new ArrayList<>();
        phoneNumbers.add(phoneNum);
        List<Map<String, String>> params = new ArrayList<>();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", code);
        params.add(paramMap);
        List<String> name = new ArrayList<>();
        name.add("泰山科技");
        PhoneParam emsParam = new PhoneParam(SMS_RANDOM_CODE, JSONUtil.toJsonStr(params), JSONUtil.toJsonStr(phoneNumbers), JSONUtil.toJsonStr(name));
        sendBatchSms(emsParam);
        return code;
    }

    /**
     * 发生短信 - 多条
     */
    public void sendBatchSms(PhoneParam phoneParam) throws Exception {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendBatchSms");
        request.putQueryParameter("RegionId", commonPhoneProperties.getRegionId());
        request.putQueryParameter("PhoneNumberJson", phoneParam.getPhoneNumbers());
        request.putQueryParameter("SignNameJson", phoneParam.getSignNameJson());
        request.putQueryParameter("TemplateCode", phoneParam.getTemplateCode());
        request.putQueryParameter("TemplateParamJson", phoneParam.getTemplateParam());
        CommonResponse response = client.getCommonResponse(request);
        log.info("sendSMSMessage, response is {}", JSONUtil.toJsonStr(response));
    }

    /**
     * 发生短信 - 单挑
     */
    public void sendSms(PhoneParam phoneParam) throws Exception {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", commonPhoneProperties.getRegionId());
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phoneParam.getPhoneNumbers());
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
        request.putQueryParameter("SignName", phoneParam.getSignNameJson());
        // 短信模板ID
        request.putQueryParameter("TemplateCode", phoneParam.getTemplateCode());
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", phoneParam.getTemplateParam());
        CommonResponse commonResponse = client.getCommonResponse(request);
        log.info("commonResponse is {}", JSONUtil.toJsonStr(commonResponse));
    }
}
